//
//  CollectionViewController.m
//  TasteEnjoy
//
//  Created by XuRui on 16/5/4.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#import "CollectionViewController.h"

@interface CollectionViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) BaseTableView *tableView;

@property (nonatomic, retain) NSArray *arrData;
@end

@implementation CollectionViewController

#pragma mark - ------------ 内存管理 ---------------
- (void)dealloc
{
    _tableView.delegate = nil;
    _tableView.dataSource= nil;
    [_tableView release];
    [_arrData release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"我的收藏";
    
  
    //创建数据库查询出来的数据
    self.arrData = [[XrDataBaseManager shareInstace]select];
    
    if (_arrData.count == 0) {
        //创建弹出视图
        
        
        UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有收藏过哦, 快去收藏吧" preferredStyle:UIAlertControllerStyleAlert];
        
        //模态推出
        [self presentViewController:alertVc animated:YES completion:^{
            UIImageView *img = [[UIImageView alloc]initWithFrame:SCREEN_BOUNDS];
            img.image = [UIImage imageNamed:@"yin0.jpg"];
            [self.view addSubview:img];
            
        }];
   
        //自动收回 尽量不用perform 会不好内存管理
        
//        dispatch_async(dispatch_get_main_queue(), ^{
//            
//            [self autoBackHome:alertVc];
//        });
        
        
        [self performSelector:@selector(autoBackHome:) withObject:alertVc afterDelay:2];
    }
    
    [self createTableView];

    
}


#pragma mark - ------------ 创建Tableview ---------------
- (void)createTableView
{
    
    self.tableView = [[BaseTableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
/** 夜间 */
    _tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor] ,[UIColor grayColor]);
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
    
    [_tableView release];
    
}


#pragma mark - ------------ 返回个数 ---------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _arrData.count;
}

#pragma mark - ------------ cell赋值 ---------------

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    /** 夜间 */
    cell.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor] ,[UIColor grayColor]);
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    EnjoyModel *m = _arrData[indexPath.row];
    cell.textLabel.text = m.short_name;
    cell.textLabel.font = [UIFont systemFontOfSize:15];
/** 夜间 */
    cell.textLabel.dk_textColorPicker = DKColorWithColors([UIColor grayColor],[UIColor lightGrayColor]);
    return cell;
    
    
}

#pragma mark - ------------ 收藏点击 ---------------
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    EWebViewController *deatail = [[EWebViewController alloc]init];
    
     EnjoyModel *m = _arrData[indexPath.row];
    deatail.DetailID = m.product_id;
    [self.navigationController pushViewController:deatail animated:YES];
  
    [deatail release];

}


#pragma mark - ------------ alert自动收回 ---------------
- (void)autoBackHome:(UIAlertController *)alert
{
    
    [alert dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
